package com.derive.education_service.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.derive.education_service.common.global.Paper;
import com.derive.education_service.common.global.ResponseMessage;
import com.derive.education_service.entity.Admin;
import com.derive.education_service.mapper.AdminMapper;
import com.derive.education_service.web.admin.bean.dto.AdminAddDto;
import com.derive.education_service.web.admin.bean.dto.AdminUpDto;
import com.derive.education_service.web.admin.bean.dto.QueryParam;
import com.derive.education_service.web.admin.bean.vo.AdminInfoVo;
import com.derive.education_service.web.admin.service.AdminMegService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 管理员管理
 */
@Service
@Slf4j
public class AdminMegServiceImpl implements AdminMegService {

    @Autowired
    private AdminMapper adminMapper;


    @Override
    public ResponseMessage addAdminInfo(HttpServletRequest request, AdminAddDto adminAddDto) {
        // 转换数据
        ObjectMapper objectMapper = new ObjectMapper();
        AdminInfoVo adminInfoVo = objectMapper
                .convertValue(request.getSession().getAttribute(request.getHeader("USESSIONID")), AdminInfoVo.class);
        // 校验是否具备操作权限
        if (adminInfoVo.getRoleId() != 1) {
            return ResponseMessage.createByErrorMessage("对不起，你没有该操作权限");
        }
        // 校验是否已存在账户
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", adminAddDto.getAccount());
        Admin isAdmin = adminMapper.selectOne(queryWrapper);
        if (isAdmin != null) {
            return ResponseMessage.createByErrorMessage("账号已存在");
        }
        // 数据转换
        Admin admin = new Admin();
        BeanUtils.copyProperties(adminAddDto, admin);
        admin.setRoleId(3); // 默认权限
        admin.setIsState(true);
        admin.setIsOnline(false);
        admin.setLoginFailure(0);
        admin.setCreateTime(new Date());
        admin.setUpdateTime(new Date());

        int row = adminMapper.insert(admin);
        if (row <= 0) {
            return ResponseMessage.createByErrorMessage("新增异常");
        }
        return ResponseMessage.createBySuccessMessage("账号" + adminAddDto.getAccount() + "成功");
    }

    /**
     * 删除先设定为失效吧
     * @param request
     * @param adminId
     * @return
     */
    @Override
    public ResponseMessage delAdmin(HttpServletRequest request, int adminId) {
        // 转换数据
        ObjectMapper objectMapper = new ObjectMapper();
        AdminInfoVo adminInfoVo = objectMapper
                .convertValue(request.getSession().getAttribute(request.getHeader("USESSIONID")), AdminInfoVo.class);
        // 校验是否具备操作权限
        if (adminInfoVo.getRoleId() != 1) {
            return ResponseMessage.createByErrorMessage("对不起，你没有该操作权限");
        }
        return null;
    }

    @Override
    public ResponseMessage upAdmin(HttpServletRequest request, AdminUpDto adminUpDto) {
        Admin admin = new Admin();
        BeanUtils.copyProperties(adminUpDto, admin);
        admin.setUpdateTime(new Date());

        int row = adminMapper.updateById(admin);

        if (row <= 0) {
            return ResponseMessage.createByErrorMessage("编辑信息异常");
        }
        return ResponseMessage.createBySuccessMessage("编辑成功");
    }

    /**
     *
     * @param request
     * @param roleId
     * @param adminId -- 需要分配权限的管理id
     * @return
     */
    @Override
    public ResponseMessage allotRole(HttpServletRequest request, int roleId, int adminId) {
        // 转换数据
        ObjectMapper objectMapper = new ObjectMapper();
        AdminInfoVo adminInfoVo = objectMapper
                .convertValue(request.getSession().getAttribute(request.getHeader("USESSIONID")), AdminInfoVo.class);
        // 校验是否具备操作权限
        if (adminInfoVo.getRoleId() != 1) {
            return ResponseMessage.createByErrorMessage("对不起，你没有该操作权限");
        }
        // 超管角色和默认账号无法分配
        if (roleId == 1 || roleId == 3) {
            return ResponseMessage.createByErrorMessage("该角色唯一，无法分配");
        }
        Admin admin = new Admin();
        admin.setAdminId(adminId);
        admin.setRoleId(roleId);

        int row = adminMapper.updateById(admin);
        if (row <= 0) {
            return ResponseMessage.createByErrorMessage("更改异常");
        }
        return ResponseMessage.createBySuccessMessage("权限分配成功");
    }

    @Override
    public ResponseMessage banAdmin(HttpServletRequest request, int adminId) {
        // 转换数据
        ObjectMapper objectMapper = new ObjectMapper();
        AdminInfoVo adminInfoVo = objectMapper
                .convertValue(request.getSession().getAttribute(request.getHeader("USESSIONID")), AdminInfoVo.class);
        // 校验是否具备操作权限
        if (adminInfoVo.getRoleId() != 1) {
            return ResponseMessage.createByErrorMessage("对不起，你没有该操作权限");
        }
        if (adminId == 1) {
            return ResponseMessage.createByErrorMessage("超管用户无法禁用");
        }
        Admin admin = new Admin();
        admin.setAdminId(adminId);
        admin.setIsState(false);

        int row = adminMapper.updateById(admin);
        if (row <= 0) {
            return ResponseMessage.createByErrorMessage("禁用异常");
        }
        return ResponseMessage.createBySuccessMessage("账号已禁用");
    }

    @Override
    public ResponseMessage startAdmin(HttpServletRequest request, int adminId) {
        // 转换数据
        ObjectMapper objectMapper = new ObjectMapper();
        AdminInfoVo adminInfoVo = objectMapper
                .convertValue(request.getSession().getAttribute(request.getHeader("USESSIONID")), AdminInfoVo.class);
        // 校验是否具备操作权限
        if (adminInfoVo.getRoleId() != 1) {
            return ResponseMessage.createByErrorMessage("对不起，你没有该操作权限");
        }
        if (adminId == 1) {
            return ResponseMessage.createByErrorMessage("超管用户无法禁用");
        }
        Admin admin = new Admin();
        admin.setAdminId(adminId);
        admin.setIsState(true);

        int row = adminMapper.updateById(admin);
        if (row <= 0) {
            return ResponseMessage.createByErrorMessage("启用异常");
        }
        return ResponseMessage.createBySuccessMessage("账号已启用");
    }

    /**
     * 获取管理员列表数据
     * @return
     */
    @Override
    public ResponseMessage getAdminList(HttpServletRequest request, QueryParam queryParam) {
        // 转换数据
        ObjectMapper objectMapper = new ObjectMapper();
        AdminInfoVo adminInfoVo = objectMapper
                .convertValue(request.getSession().getAttribute(request.getHeader("USESSIONID")), AdminInfoVo.class);
        // 校验是否具备操作权限
        if (adminInfoVo.getRoleId() != 1) {
            return ResponseMessage.createByErrorMessage("对不起，你没有该操作权限");
        }

        // 封装分页参数
        Page<Admin> page = new Page<>();
        page.setCurrent(queryParam.getCurrent());
        page.setSize(queryParam.getSize());

        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();

        if (queryParam.getAdminName() != null && !queryParam.getAdminName().isEmpty()) {
            queryWrapper.like("admin_name", queryParam.getAdminName());
        }

        if (queryParam.getIsState() != null) {
            queryWrapper.eq("is_state",queryParam.getIsState());
        }

        Page<Admin> adminPage = adminMapper.selectPage(page, queryWrapper);

        Paper<Admin> paper = new Paper<>();
        paper.setCurrent(adminPage.getCurrent());
        paper.setTotal(adminPage.getTotal());
        paper.setSize(adminPage.getSize());
        paper.setPages(adminPage.getPages());
        paper.setData(adminPage.getRecords());

        return ResponseMessage.createBySuccess(paper);
    }
}
